package cn.tworice.cryption.config;

import cn.tworice.cryption.exception.CryptionException;
import cn.tworice.cryption.service.CryptService;
import cn.tworice.cryption.service.impl.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@Configuration
@Slf4j
public class CryptionConfig {
    @Resource
    private CryptionProperties cryptionProperties;

    @Bean
    public CryptService cryptService() throws Exception {
        if(cryptionProperties.getEnable()) log.info("启用数据接口加密");
        switch (cryptionProperties.getType()) {
            case "AES":
                return new AesServiceImpl(cryptionProperties.getKey());
            case "DES":
                return new DesServiceImpl(cryptionProperties.getKey());
            case "Base64":
                return new Base64ServiceImpl();
            case "RSA+AES":
                return new AesServiceImpl(cryptionProperties.getKey());
            case "RSA+AES-GCM":
                return new RsaAndAesServiceImpl(cryptionProperties);
            case "SM4":
                return new SM4ServiceImpl(cryptionProperties.getKey());
            case "SM2+SM4":
                return new SM4ServiceImpl(cryptionProperties.getKey());
            default:
                throw new CryptionException("找不到加密类型： " + cryptionProperties.getType());
        }
    }
}
